//  middleware只在服务段渲染
const commonPathDict = {
  'CUSTOMER': '/server'
}
export default async function ({ store, route, $api }) {
  // 在服务端获取用户信息
  if (route.query.app_id && route.query.client_id && route.query.ticket) {
    let baseURL = ''
    if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'uat') {
      baseURL = 'https://bisapi.dev.shijiayun.cn/'
    } else {
      baseURL = 'https://bisapi.shijiayun.cn/'
    }
    let res = await $api.checkBrainLogin({
      app_id: route.query.app_id,
      client_id: route.query.client_id,
      ticket: route.query.ticket,
    })
    // console.log('middle ware checkBrainLogin res:', res)
    res = res.data
    store.commit('auth/SetToken', res.token)
    store.commit('auth/SetRole', res.account.identity)
    store.commit('auth/SetCommonUrlPath', commonPathDict[res.account.identity])
    store.commit('auth/SetAccountId', res.account.id)
    if (!store.getters.oss_domain) {
      const ossInfo = await $api.getOssDomain()
      store.commit('auth/SetDomain', ossInfo.domain)
    }
  }
  if (route.query.token) {
    store.commit('auth/SetToken', route.query.token) 
    let res = await $api.getLoginAccount()
    // console.log('middle ware getLoginAccount:', res)
    const info = res.account
    store.commit('auth/SetRole', info.identity)
    store.commit('auth/SetCommonUrlPath', commonPathDict[info.identity])
    store.commit('auth/SetAccountId', info.id)
    if (!store.getters.oss_domain) {
      const ossInfo = await $api.getOssDomain()
      store.commit('auth/SetDomain', ossInfo.domain)
    }
  }
}